package com.huatai.datadevelop.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.huatai.common.api.R;
import com.huatai.common.error.exception.ServiceException;
import com.huatai.datadevelop.entity.InterfaceTableConfigEntity;
import com.huatai.datadevelop.entity.TableConfigEntity;
import com.huatai.datadevelop.service.InterfaceTableConfigService;
import com.huatai.datadevelop.service.TableConfigLogService;
import com.huatai.datadevelop.service.TableConfigService;
import com.huatai.datadevelop.utils.PageUtils;
import com.huatai.datadevelop.vo.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(value = "静态数据配置模块",tags = "静态数据配置模块")
@Slf4j
@RestController
@RequestMapping("/table/config")
public class TableConfigController {

    @Autowired
    private TableConfigService tableConfigService;
    @Autowired
    private InterfaceTableConfigService interfaceTableConfigService;
    @Autowired
    private TableConfigLogService tableConfigLogService;

    /**
     * 获取表列表
     * @return
     */
	@ApiOperation("获取表列表")
    @GetMapping("getListToConfig")
    public R getListToConfig(){
        List<TableConfigEntity> list = this.tableConfigService.list();
        List<HashMap<String,Object>> mapList = new ArrayList<>();
        list.stream().forEach(p->{
            HashMap<String, Object> hashMap = new HashMap<>();
            hashMap.put("name",p.getName());
            hashMap.put("tableName",p.getTableName());
            hashMap.put("table_id",p.getId());
            mapList.add(hashMap);
        });
        return R.data(mapList);
    }

    /**
     * 获取表名称是否合规
     * @param vo
     * @return
     */
	@ApiOperation("获取表名称是否合规")
    @PostMapping("getTableNameCompliance")
    public R getTableNameCompliance(@RequestBody TableNameComplianceVO vo){
        HashMap<String, Object> tableNameCompliance = this.tableConfigService.getTableNameCompliance(vo.getTableName());
        return R.data(tableNameCompliance);
    }

    /**
     * 获取数据列表
     * @param params
     * @return
     */
	@ApiOperation("获取数据列表")
    @PostMapping("list")
    public R list(@RequestBody Map<String, Object> params){
        return R.data(this.tableConfigService.queryPage(params));
    }

    /**
     * 删除数据单个或多个
     * @param vo
     * @return
     */
	@ApiOperation("删除数据单个或多个")
    @PostMapping("deleteByIds")
    public R delete(@RequestBody DeleteVO vo){
		Boolean flag = tableConfigService.deleteByIds(vo.getIds());
		return flag? R.success("删除成功"): R.fail("删除失败");
    }

    /**
     * 新增或者更新
     * @param entity
     * @return
     * @throws SQLException
     */
	@ApiOperation("新增或者更新")
    @PostMapping("saveOrUpdate")
    public R saveOrUpdate(@RequestBody TableConfigEntity entity) throws SQLException {
        Boolean aBoolean = this.tableConfigService.saveOrUpdateEntity(entity);
        return aBoolean?R.success("成功"):R.fail("失败");
    }

    /**
     * 根据表英文名称获取表中所有数据
     * @param params
     * @return
     */
	@ApiOperation("根据表英文名称获取表中所有数据")
    @PostMapping("/getDataListByTableName")
    public R getDataListById(@RequestBody Map<String, Object> params){
        return R.data(this.tableConfigService.getDataListById(params));
    }

    /**
     * 根据表英文名称新增或更新数据
     * @param vo
     * @return
     */
	@ApiOperation("根据表英文名称新增或更新数据")
    @PostMapping("/saveOrUpdataBatchByTableName")
    public R saveOrUpdataBatchByTableName(@RequestBody UpdateTableVO vo){
		this.tableConfigService.saveOrUpdataBatchByTableName(vo);
		return R.success("成功");
    }

    /**
     * 根据表名清空数据
     * @param
     * @return
     */
	@ApiOperation("根据表名清空数据")
    @PostMapping("/deleteAllDataByTableId")
    public R deleteAllDataByTableId(@RequestBody JSONObject data){
		Boolean aBoolean=this.tableConfigService.deleteAllDataByTableId(data);
		return aBoolean?R.success("成功"):R.fail("失败");
    }

    /**
     * 获取英文名称是否合规
     * @param interfaceNameComplianceVO
     * @return
     */
	@ApiOperation("获取英文名称是否合规")
    @PostMapping("getInterfaceNameCompliance")
    public R getInterfaceNameCompliance(@RequestBody InterfaceNameComplianceVO interfaceNameComplianceVO){
        return R.data(this.interfaceTableConfigService.getTableNameCompliance(interfaceNameComplianceVO.getInterfaceName()));
    }
    /**
     * 接口调用配置新增或修改
     * @return
     */
	@ApiOperation("接口调用配置新增或修改")
    @PostMapping("/interfaceConfigSaveOrUpdate")
    public R interfaceConfigSaveOrUpdate(@RequestBody InterfaceTableConfigEntity entity){
        return R.data(this.interfaceTableConfigService.saveOrUpdate(entity));
    }

    /**
     * 接口调用配置
     * @return
     */
	@ApiOperation("接口调用配置")
    @PostMapping("/interfaceConfigList")
    public R interfaceConfigList(@RequestBody Map<String, Object> params){
        return R.data(this.interfaceTableConfigService.queryPage(params));
    }

    /**
     * 批量删除
     * @return
     */
	@ApiOperation("批量删除")
    @PostMapping("/deleteInterfaceConfigById")
    public R deleteInterfaceConfigById(@RequestBody DeleteVO vo){
        return R.data(this.interfaceTableConfigService.removeByIds(vo.getIds()));
    }

    /**
     * 根据表名称自动装配sql
     * @param vo
     * @return
     */
	@ApiOperation("根据表名称自动装配sql")
    @PostMapping("getTableNameToSQL")
    public R getTableNameToSQL(@RequestBody TableNameToSQLVO vo){
        HashMap<String, String> tableNameToSQL = this.interfaceTableConfigService.getTableNameToSQL(vo.getTableName());
        return R.data(tableNameToSQL);
    }


    /**
     * 调用数据
     * @param interfaceName
     * @param data
     * @return
     */
	@ApiOperation("调用数据")
    @PostMapping("/asyncRequestApi/{interfaceName}")
    public R asyncRequestApi(@PathVariable("interfaceName") String interfaceName,@RequestBody JSONObject data){
        QueryWrapper<InterfaceTableConfigEntity> qw = new QueryWrapper<>();
        qw.eq("interface_name",interfaceName);
        InterfaceTableConfigEntity entity = this.interfaceTableConfigService.getOne(qw);
        if(entity==null){
            throw new ServiceException("查无>>>>"+interfaceName+"<<<<此接口，请核查");
        }
        return R.data(this.interfaceTableConfigService.asyncRequestApi(data,entity,new HashMap<String,Object>()));
    }

    /**
     * 导入数据
     * @param
     * @param
     * @return
     */
	@ApiOperation("导入数据")
    @PostMapping("/importExcel")
    public R importExcel(@RequestParam("file") MultipartFile file,
                         @RequestParam("tableId") String tableId) throws IOException {
		this.interfaceTableConfigService.importExcel(file, tableId);
		return R.success("操作成功");
    }

    /**
     * 日志信息列表
     * @param params
     * @return
     */
	@ApiOperation("日志信息列表")
    @PostMapping("tableConfigLogList")
    public R tableConfigLogList(@RequestBody Map<String,Object> params){
        PageUtils queryPage = this.tableConfigLogService.queryPage(params);
        return R.data(queryPage);
    }

    /**
     * 异常数据重新入库
     * @param
     * @return
     */
	@ApiOperation("异常数据重新入库")
    @PostMapping("tableConfigLogAbnormalData")
    public R tableConfigLogAbnormalData(@RequestBody TableConfigLogVo vo){
		Boolean aBoolean = this.tableConfigLogService.tableConfigLogAbnormalData(vo);
		return aBoolean ? R.success("成功") : R.fail("失败");
    }
}
